package com.ymatou.iapi.godmch.testcase;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonSyntaxException;
import com.ymatou.iapi.godmch.parameter.PointConfig;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.getPointConfigBean;
import com.ymatou.iapi.godmch.parameter.setBizConfigBean;
import com.ymatou.iapi.godmch.parameter.setPointConfigBean;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getPointConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.point_change_config;
import com.ymttest.database.model.godmch.point_change_configExample;
import com.ymttest.database.model.godmch.point_change_type_dic;
import com.ymttest.database.model.godmch.point_change_type_dicExample;
import com.ymttest.database.sqlwapper.godmch.point_change_configWapper;
import com.ymttest.database.sqlwapper.godmch.point_change_type_dicWapper;
import com.ymttest.utils.EnvSetup;
/**
*获取积分配置
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getPointConfig{
	private static getPointConfigBean bean;
	private static getPointConfigCall call;
	Integer agent_promote_mchId=Integer.parseInt(EnvSetup.getData("agent_promote_mchId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取积分配置");
	}

	@Before
	public void caseUp() {
		bean=new getPointConfigBean();
		call=new getPointConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify(boolean enabled) throws JsonSyntaxException, JSONException, InterruptedException
	{
		List<PointConfig> return_configs=call.getPointConfigList();
		//point_change_config
		point_change_configWapper point_change_configWapper =new point_change_configWapper();
		point_change_configExample example=new point_change_configExample();
		example.createCriteria().andMch_idEqualTo(bean.getMchId());
		List<point_change_config> DB_coinfigs=point_change_configWapper.selectByExample(example);
		//point_change_type_dic
		point_change_type_dicWapper point_change_type_dicWapper =new point_change_type_dicWapper();
		point_change_type_dicExample type_example=new point_change_type_dicExample();
		type_example.createCriteria().andChange_type_idEqualTo(DB_coinfigs.get(0).getChange_type_id());
		List<point_change_type_dic> DB_types=point_change_type_dicWapper.selectByExample(type_example);
		
		
		Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
		Logger.verifyEquals(DB_coinfigs.get(0).getChange_type_id(), return_configs.get(0).getPointType(), "验证PointType");
		Logger.verifyEquals(DB_coinfigs.get(0).getPoint_value(), return_configs.get(0).getPoint(), "验证Point");
		Logger.verifyEquals(DB_types.get(0).getChange_type_title(), return_configs.get(0).getPointTypeTitle(), "验证PointTypeTitle");
		Logger.verifyEquals(enabled, call.getEnabled(), "验证Enabled");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getPointConfig_001() {
		Logger.start(true,"获取积分配置-未开启积分");
		try {
            
		   createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
           createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
           Integer mchId=createIndepMchCall.getmchId();
           
			bean.setMchId(mchId);
			call.setData(bean);
			call.callService();			
			
			List<PointConfig> return_configs=call.getPointConfigList();
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Logger.verifyEquals(100, return_configs.get(0).getPointType(), "验证PointType");
			Logger.verifyEquals(0, return_configs.get(0).getPoint(), "验证Point");
			Logger.verifyEquals("分享", return_configs.get(0).getPointTypeTitle(), "验证PointTypeTitle");
			Logger.verifyEquals(false, call.getEnabled(), "验证Enabled");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getPointConfig_002() {
		Logger.start(true,"获取积分配置-开启积分");
		try {
            
			//Integer mchId=agent_promote_mchId;
            //开启商户积分
            setBizConfigBean configBean=new setBizConfigBean();
            configBean.setBizId(agent_promote_mchId);
        	Map<String,String> map=new HashedMap();
			map.put("enable_point", "1");
			configBean.setBizConfigMap(map);
            GodmchManageCallService.setBizConfig(configBean);
            Integer agentId= GodmchManageCallService.applyAndapproveAgent(agent_promote_mchId).getAgentId();
            //point_change_config
            GodmchManageCallService.setPointConfig(agent_promote_mchId);
            
			bean.setMchId(agent_promote_mchId);
			call.setData(bean);
			call.callService();			
			
			check_verify(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getPointConfig_003() {
		Logger.start(true,"获取积分配置-删除配置");
		try {
            
			//setPointConfigBean configBean=GodmchManageCallService.setPointConfig();
			//bean.setMchId(configBean.getMchId());
			bean.setMchId(501395803);
			call.setData(bean);
			call.callService();			
			
			List<PointConfig> return_configs=call.getPointConfigList();
			Logger.verifyEquals("200", call.getStatusCode1(), "验证statusCode");
			Logger.verifyEquals(100, return_configs.get(0).getPointType(), "验证PointType");
			Logger.verifyEquals(0, return_configs.get(0).getPoint(), "验证Point");
			Logger.verifyEquals("分享", return_configs.get(0).getPointTypeTitle(), "验证PointTypeTitle");
			Logger.verifyEquals(false, call.getEnabled(), "验证Enabled");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}